package com.am.split2cls;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.stream.Stream;

import org.omg.CORBA.PUBLIC_MEMBER;

/**
 * Created by sss on 2016/8/3.
 */

public class SplitFile2Cls {

    public  int nClassNumber = 0;       // class number, class A01, A02
    public  String strAlphaBet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    //	public  String strPackageName = "com.n.b.c";		//package name
    public  String strClassName = null;
    public  String strClsTail = "    \r\n};\r\n" + "}\r\n";
    public  String OUT_DIR = null;

//	public static String strClassHeader = "public class" +  A  {
//		public static byte[] ELF_BYTES = new byte[]{""
//				+ "";

    public  String genClsHeaderString(){
        strClassName = generateClassNameString(nClassNumber);
        String strClsHeader = "public class " + generateClassNameString(nClassNumber) + "{\r\n" +
                "    public static byte[] ELF_BYTES = new byte[]{\r\n";
        nClassNumber++;
        return strClsHeader;
    }

    public  String generateClassNameString(int n) {
        String strRet = "";
//        strRet += strAlphaBet.charAt(n);
        strRet = strRet + "A" + new Integer(nClassNumber).toString();
        return strRet;
    }

    public  boolean writeString2File(String strData) {
        File file = new File(OUT_DIR + strClassName + ".java");
        FileWriter fw;
        try {
            fw = new FileWriter(file, true);
            BufferedWriter bWriter = new BufferedWriter(fw);
            // write file by each line
            bWriter.write(strData);

            bWriter.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return true;
    }


    //write file
    public  boolean writePortion2File(String[] strPortion) {
        File file = new File(OUT_DIR + strClassName + ".java");
        FileWriter fw;
        try {
            fw = new FileWriter(file, true);
            BufferedWriter bWriter = new BufferedWriter(fw);

            // write file by each line
            for (String strEachLine : strPortion){
                if (strEachLine == null) {
                    break;
                }
                bWriter.write(strEachLine);
                bWriter.write("\r\n");
            }

            bWriter.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return true;
    }

    public  static void main(String[] args) {
        //test

        //test end

        SplitFile2Cls myof = new SplitFile2Cls();
        String strInFile = args[0];	// input file

        if (strInFile == null) {
            System.out.println("Please input infile name!");
            System.exit(-1);
        }

        String strOutFile = null;	// input file
        File infile = new File(strInFile);


        //get file dir ==> output dir
        String strDir = infile.getParent();	//
        String strOutDir = strDir + "//" + "handled//";
        myof.OUT_DIR = strOutDir;	//set outdir

        // output class code
        String strCodeFile = strOutDir + "ClassCode.txt";

        //mk dirs
        File fileOutDir = new File(strOutDir);
        if (fileOutDir.exists()) {
            fileOutDir.delete();
        }

        fileOutDir.mkdirs();	//mk dirs

        FileReader fReader;

        //
        File fileCodeFile = null;

        try {
            fReader = new FileReader(infile);
            BufferedReader br = new BufferedReader(fReader);
            int nNumberLines = myof.getFileLinesFromBufferReader(br); // get file

            // total lines
            br.close();
            fReader.close();

            //re open
            fReader = new FileReader(infile);
            br = new BufferedReader(fReader);

            // output file
//            File fileCodeFile = new File(strCodeFile);

            int nNumPortion = nNumberLines / 40; // separate to 20 portion
            int nlines = 0;
            String[] strPortion = new String[nNumPortion];

            while (true) {
                if (nlines == nNumPortion) {
                    // write file
                    // gen class info
                    String strHeader = myof.genClsHeaderString();
                    myof.writeString2File(strHeader);
                    myof.writePortion2File(strPortion);
                    myof.writeString2File(myof.strClsTail);

                    //
//                    strCodeFile = ;

                    //
                    nlines = 0;
                    strPortion = new String[nNumPortion];
                    continue;
                }

                String strline = br.readLine(); // line content
                if (strline == null) { // quit while
                    //
                    String strHeader = myof.genClsHeaderString();
                    myof.writeString2File(strHeader);
                    myof.writePortion2File(strPortion);
                    myof.writeString2File(myof.strClsTail);

                    break;
                }

                strPortion[nlines] = strline; // save to portion
                nlines++;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //
    private  int getFileLinesFromBufferReader(BufferedReader br) {
        int nLines = 0;
        try {
            while (br.readLine() != null) {
                nLines++;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return nLines;
    }
}
